package com.battery.web.controller.system.battery;


import com.battery.common.utils.DateUtils;
import com.battery.system.domain.VO.StatisticsVO;
import com.battery.system.service.battery.IUsedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private IUsedService usedService;

    @PostMapping("/chart")
    @ResponseBody
    public Map<String, Object> toChart() {
        List<StatisticsVO> statisticsVOS = usedService.selectStatisticsVO();
        String turnover = "";
        String usedCounts = "";
        BigDecimal zbz = BigDecimal.ZERO;
        Long ybz = 0l;
        if (statisticsVOS != null && statisticsVOS.size() > 0) {
            int size = statisticsVOS.size();
            StatisticsVO statisticsVO = null;
            for (int i = 0; i < size; i++) {
                statisticsVO = statisticsVOS.get(i);
                String date = statisticsVO.getDate();
                String[] dateArr = date.split("-");
                if (i + 1 == size) {
                    turnover +=
                            "[gd(" + dateArr[0] + ", " + dateArr[1] + ", " + dateArr[2] + "), " + statisticsVO.getAmountPaid() + "]";

                    usedCounts +=
                            "[gd(" + dateArr[0] + ", " + dateArr[1] + ", " + dateArr[2] + "), " + statisticsVO.getNumCount() + "]";
                } else {
                    turnover +=
                            "[gd(" + dateArr[0] + ", " + dateArr[1] + ", " + dateArr[2] + "), " + statisticsVO.getAmountPaid() + "],";

                    usedCounts +=
                            "[gd(" + dateArr[0] + ", " + dateArr[1] + ", " + dateArr[2] + "), " + statisticsVO.getNumCount() + "],";
                }
                if (statisticsVO.getAmountPaid().subtract(zbz).compareTo(BigDecimal.ZERO) > 0) {
                    zbz = statisticsVO.getAmountPaid();
                }
                if (statisticsVO.getNumCount() > ybz) {
                    ybz = statisticsVO.getNumCount();
                }
            }
        }

        // 左边轴

        // 右边轴


        Map<String, Object> map = new HashMap<>();
        map.put("turnover", "[" + turnover + "]");
        map.put("usedCounts", "[" + usedCounts + "]");
        map.put("zbz", zbz);
        map.put("ybz", ybz);
        return map;
    }
}
